package com.aorise.controller.exposeInterfaces;

import com.aorise.model.expose.DevInfo;
import com.aorise.model.expose.DevState;
import com.aorise.model.expose.EventInfo;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Auther: zhouhao
 * @Date: 2019/7/10
 * @Description:
 */
@Api(description ="CMU暴露接口")
@Slf4j
@RestController
public class CmuInfoController {


    /**
     * CMU将IMB每次上电发送设备信息发至后端
     * HTTP 方式：PUT
     * API 路径：/DevInfo
     * 方法名：getDevInfo
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "CMU将IMB每次上电发送设备信息发至后端", httpMethod = "PUT", response = String.class, notes = "CMU将IMB每次上电发送设备信息发至后端,提供后端保存设备信息")
    @RequestMapping(value = "/DevInfo", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDevInfo(@RequestBody @Validated DevInfo devInfo, BindingResult result) {
        log.debug("Request RESTful API:getDevInfo");

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            return new JsonResponseData(true, StatusDefineMessage.GetMessage(StatusDefine.SUCCESS),
                    StatusDefine.SUCCESS, "", devInfo).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:CmuInfoController. function:getDevInfo...msg:getDevInfo DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:CmuInfoController. function:getDevInfo...msg:Get getDevInfo Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }


    /**
     * CMU将IMB根据设定的周期主动上传的测量值和状态信息上传到后端
     * HTTP 方式：PUT
     * API 路径：/DevState
     * 方法名：getDevState
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "CMU将IMB根据设定的周期主动上传的测量值和状态信息上传到后端", httpMethod = "PUT", response = String.class, notes = "CMU将IMB根据设定的周期主动上传的测量值和状态信息上传到后端,提供CMU使用")
    @RequestMapping(value = "/DevState", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDevState(@RequestBody @Validated DevState devState, BindingResult result) {
        log.debug("Request RESTful API:getDevState");

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            return new JsonResponseData(true, StatusDefineMessage.GetMessage(StatusDefine.SUCCESS),
                    StatusDefine.SUCCESS, "", devState).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:CmuInfoController. function:getDevState...msg:getDevState DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:CmuInfoController. function:getDevState...msg:getDevState Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * CMU将IMB根据设定的周期主动上传的测量值和状态信息上传到后端
     * HTTP 方式：PUT
     * API 路径：/EventInfo
     * 方法名：getEventInfo
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "CMU将IMB根据设定的周期主动上传的测量值和状态信息上传到后端", httpMethod = "PUT", response = String.class, notes = "CMU将IMB根据设定的周期主动上传的测量值和状态信息上传到后端,提供CMU使用")
    @RequestMapping(value = "/EventInfo", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEventInfo(@RequestBody @Validated EventInfo eventInfo, BindingResult result) {
        log.debug("Request RESTful API:getDevState");

        try {
            if (result.hasErrors()) {
                return JsonResponseData.ofMessage(StatusDefine.DATA_FORMAT_ERROR, result.getFieldError().getDefaultMessage());
            }
            return new JsonResponseData(true, StatusDefineMessage.GetMessage(StatusDefine.SUCCESS),
                    StatusDefine.SUCCESS, "", eventInfo).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:CmuInfoController. function:getEventInfo...msg:getEventInfo DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:CmuInfoController. function:getEventInfo...msg:getEventInfo Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

}
